#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(libevbase, 1.0.2, SounOS@gmail.com)
AC_CONFIG_SRCDIR([src/evbase.c])
AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE
# Checks for programs.
AC_PROG_CC
AC_PROG_LIBTOOL
AM_PROG_CC_C_O

# Checks for libraries.
#pthread
havepthread=no
AC_CHECK_LIB(pthread,pthread_create,havepthread=yes)
if test "$havepthread" = "yes" ; then
    CPPFLAGS="${CPPFLAGS} -DHAVE_PTHREAD"
    LDFLAGS="${LDFLAGS} -lpthread"
    LIBS="${LIBS} -lpthread"
fi

#semaphore
#havesemaphore=no
#AC_CHECK_LIB(c, sem_timedwait, havesemaphore=yes)
#if test "$havesemaphore" = "yes" ; then
#    CPPFLAGS="${CPPFLAGS} -DHAVE_SEMAPHORE"
#fi

#check port
haveevport=no
AC_CHECK_FUNCS(port_create, [haveevport=yes], )
if test "$haveevport" = "yes" ; then
    CPPFLAGS="${CPPFLAGS} -DHAVE_EVPORT"
    AC_DEFINE(HAVE_EVPORT, 1,
              [Define if your system supports event ports])
    AC_LIBOBJ(evport)
fi
#check select
haveevselect=no
AC_CHECK_FUNCS(select, [haveevselect=yes], )
if test "$haveevselect" = "yes" ; then
    CPPFLAGS="${CPPFLAGS} -D HAVE_EVSELECT"
    AC_DEFINE(HAVE_EVSELECT, 1,
              [Define if your system supports select])
    AC_LIBOBJ(select)
fi
#check poll
haveevpoll=no
AC_CHECK_FUNCS(poll, [haveevpoll=yes], )
if test "$haveevpoll" = "yes" ; then
    CPPFLAGS="${CPPFLAGS} -DHAVE_EVPOLL"  
    AC_DEFINE(HAVE_EVPOLL, 1,
              [Define if your system supports poll])
    AC_LIBOBJ(poll)
fi
#check signal
haveevrtsig=no
AC_CHECK_FUNCS(signal, [haveevrtsig=yes], )
if test "$haveevrtsig" = "yes" ; then
    CPPFLAGS="${CPPFLAGS} -D HAVE_EVRTSIG"
    AC_DEFINE(HAVE_EVRTSIG, 1,
              [Define if your system supports signal])
    AC_LIBOBJ(signal)
fi

#check epoll
haveevepoll=no
AC_CHECK_FUNCS(epoll_create, [haveevepoll=yes], )
if test "$haveevepoll" = "yes" ; then
    CPPFLAGS="${CPPFLAGS} -DHAVE_EVEPOLL"
    AC_DEFINE(HAVE_EVEPOLL, 1,
              [Define if your system supports epoll])
    AC_LIBOBJ(epoll)
fi

#check kqueue
haveevkqueue=no
AC_CHECK_FUNCS(kqueue, [haveevkqueue=yes], )
if test "$haveevkqueue" = "yes" ; then
    CPPFLAGS="${CPPFLAGS} -D HAVE_EVKQUEUE"
    AC_DEFINE(HAVE_EVKQUEUE, 1,
              [Define if your system supports kqueue])
    AC_LIBOBJ(kqueue)
fi

#check devpoll
haveevdevpoll=no
AC_CHECK_FUNCS(devpoll, [haveevdevpoll=yes], )
if test "$ac_cv_header_sys_devpoll_h" = "yes" ; then
    CPPFLAGS="${CPPFLAGS} -DHAVE_EVDEVPOLL"
    AC_DEFINE(HAVE_EVDEVPOLL, 1,
              [Define if your system supports dev/poll])
    AC_LIBOBJ(devpoll)
fi

#ssl
havessl=no
AC_CHECK_LIB(ssl, SSL_read, havessl=yes)
if test "$havessl" = "yes" ; then
    CPPFLAGS="${CPPFLAGS} -DHAVE_SSL"
    LDFLAGS="${LDFLAGS} -lssl -lcrypto"
    LIBS="${LIBS} -lssl -lcrypto"
fi

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([arpa/inet.h fcntl.h netdb.h netinet/in.h stdint.h stdlib.h string.h sys/socket.h sys/time.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM

# Checks for library functions.
AC_FUNC_FORK
AC_FUNC_LSTAT
AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
AC_FUNC_SELECT_ARGTYPES
AC_FUNC_STAT
AC_CHECK_FUNCS([getpagesize gettimeofday inet_ntoa memset mkdir munmap select socket strerror])

AC_CONFIG_FILES([Makefile
                 src/Makefile])
AC_OUTPUT
